package com.tanhua.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.Admin;
import com.tanhua.domain.vo.AdminVo;
import com.tanhua.expection.BusinessException;
import com.tanhua.interceptor.AdminHolder;
import com.tanhua.mapper.AdminMapper;
import com.tanhua.uti.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AdminService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AdminMapper adminMapper;

    public ResponseEntity verification(String uuid, HttpServletResponse response) throws IOException {

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(300, 100);
        String yanZhengMa = lineCaptcha.getCode();
        System.out.println(yanZhengMa);
        // 获取验证码的内容 放入redis中 key:uuid value:验证码内容
        stringRedisTemplate.opsForValue().set("manager_code_"+uuid,yanZhengMa,5, TimeUnit.MINUTES);
        // 写给前端
        lineCaptcha.write(response.getOutputStream());
        return ResponseEntity.ok(null);

    }

    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {
        if (StrUtil.isEmpty(verificationCode) || StrUtil.isEmpty(uuid)) {
            throw new BusinessException("验证码不存在");
        }
        if (StrUtil.isEmpty(username)){
            throw  new BusinessException("用户名不存在");
        }
        if (StrUtil.isEmpty(password)){
            throw new BusinessException("密码为空");
        }
        String yanzhengma = stringRedisTemplate.opsForValue().get("manager_code_" + uuid);
        if (!StrUtil.equals(yanzhengma,verificationCode)){
            throw new BusinessException("验证码错误");
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(queryWrapper);

        if (admin==null){
            throw new BusinessException("用户不存在");
        }
        if (!admin.getPassword().equals(SecureUtil.md5(password))){
            throw new BusinessException("密码错误");
        }
        //7 清除验证码
        stringRedisTemplate.delete("manager_code_" + uuid);
        //8 生成token
        String token = JwtUtil.createToken(username,"manager-tanhua");
        //当前用户对象转成json
        String admin_json = JSON.toJSONString(admin);
        //把token放进redis
        stringRedisTemplate.opsForValue().set("manager_token_"+token,admin_json,1,TimeUnit.DAYS);
        //返回json
        Map json_map=new HashMap<String,Object>();
        json_map.put("token",token);
        return ResponseEntity.ok(json_map);
    }
    // 根据token从redis中获取用户
    public Admin tokenfromUser(String token) {
        String json_admin = stringRedisTemplate.opsForValue().get("manager_token_" + token);
        Admin admin = JSON.parseObject(json_admin, Admin.class);
        return admin;
    }

    public ResponseEntity profile() {
        Admin admin = AdminHolder.getAdmin();
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(admin,adminVo);
        return ResponseEntity.ok(adminVo);
    }

    public ResponseEntity logout() {
        Admin admin = AdminHolder.getAdmin();
        String token = admin.getToken();
        stringRedisTemplate.delete("manager_token_" + token);
        return ResponseEntity.ok(null);

    }
}
